<?php

namespace services\log;

use api\modules\v1\forms\base\DepartmentForm;
use api\modules\v1\forms\base\JobForm;
use api\modules\v1\forms\base\ProjectForm;
use api\modules\v1\forms\buss\ContentForm;
use api\modules\v1\forms\buss\ContentPublishRecordForm;
use api\modules\v1\forms\buss\TargetContentForm;
use api\modules\v1\forms\buss\ToolForm;
use api\modules\v1\forms\buss\ToolWaterForm;
use api\modules\v1\forms\member\MemberForm;
use api\modules\v1\forms\rbac\ActionForm;
use api\modules\v1\forms\rbac\MenuForm;
use api\modules\v1\forms\rbac\RoleForm;
use common\components\Service;
use common\helpers\ArrayHelper;
use common\helpers\CommonHelper;
use common\helpers\RedisHelper;
use common\models\base\CommonStatus;
use common\models\base\Department;
use common\models\base\Job;
use common\models\base\Project;
use common\models\base\Tags;
use common\models\base\ToolCategory;
use common\models\buss\Accounts;
use common\models\buss\AccountsConsume;
use common\models\buss\AccountsConsumeData;
use common\models\buss\AccountsData;
use common\models\buss\AccountsFans;
use common\models\buss\Content;
use common\models\buss\ContentPublishRecord;
use common\models\buss\Devices;
use common\models\buss\Doctors;
use common\models\buss\Platform;
use common\models\buss\Scard;
use common\models\buss\ScardRecharge;
use common\models\buss\TargetAccounts;
use common\models\buss\TargetContent;
use common\models\buss\Tool;
use common\models\buss\ToolWater;
use common\models\log\LogMemberOp;
use common\models\member\Attr;
use common\models\member\Member;
use common\models\rbac\Action;
use common\models\rbac\Menu;
use common\models\rbac\Role;
use services\base\DepartmentService;
use services\base\ProjectService;
use services\buss\AccountsService;
use services\buss\DevicesService;
use services\buss\DoctorsService;
use services\buss\PlatformService;
use services\buss\ScardService;
use services\member\MemberProjectService;
use services\member\MemberService;
use services\rbac\ActionService;
use services\rbac\MenuService;
use services\rbac\RoleService;
use Yii;
use yii\db\ActiveRecord;

class LogMemberOpService extends Service
{
    /**
     * 查询是否存在操作
     * @param int $type
     * @param $relationIds
     * @return array|ActiveRecord[]
     */
    public static function getLogMemberOpListExist(int $type, $relationIds)
    {
        return LogMemberOp::find()
            ->select(['relation_id'])
            ->where([
                'relation_id' => $relationIds,
                'type' => $type,
            ])
            ->groupBy('relation_id')
            ->indexBy('relation_id')
            ->all();
    }

    /**
     * 新增日志
     * @param int $relationId
     * @param string $content
     * @param int $type
     * @param int $memberId =0系统 >0用户
     * @return LogMemberOp
     * @throws \Exception
     */
    public static function create(int $relationId, string $content, int $type, int $memberId = 0): LogMemberOp
    {
        $logMemberOp = new LogMemberOp();
        $logMemberOp->member_id = $memberId;
        $logMemberOp->relation_id = $relationId;
        $logMemberOp->content = $content;
        $logMemberOp->type = $type;
        $logMemberOp->saveAndCheckResult();
        return $logMemberOp;
    }

    /**
     * 是否修改model
     * @param ActiveRecord $old
     * @param ActiveRecord $new
     * @param array $column
     * @return bool
     */
    public static function isEditModel(ActiveRecord $old, ActiveRecord $new, array $column = []): bool
    {
        if (empty($column)) {
            return false;
        }
        foreach ($column as $item) {
            if (is_string($old[$item]) || is_numeric($old[$item])) {
                $old[$item] = (string)$old[$item];
            }
            if (is_string($new[$item]) || is_numeric($new[$item])) {
                $new[$item] = (string)$new[$item];
            }
            if ($old[$item] !== $new[$item]) {
                return true;
            }
        }
        return false;
    }

    /**
     * 项目操作日志
     * @param ProjectForm $model
     * @param Project $new
     * @param Project|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createProjectLog(ProjectForm $model, Project $new, ?Project $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($model->getScenario()) {
            case 'create':
            case 'edit':
                $statusStr = Project::$statuss[$new->status] ?? '';
                $optionStr = "【项目：{$new->project_name} 排序：{$new->sort} 状态：{$statusStr}】";
                if ($model->getScenario() == 'create') {
                    $type = LogMemberOp::TYPE_PROJECT_CREATE;
                    $content = "新增项目;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_PROJECT_UPDATE;
                    if (self::isEditModel($old, $new, ['project_name', 'sort', 'status'])) {// 存在修改
                        $oldStatusStr = Project::$statuss[$old->status] ?? '';
                        $content = "编辑项目;由【项目：{$old->project_name} 排序：{$old->sort} 状态：{$oldStatusStr}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_PROJECT_DELETE;
                $content = "删除项目;【项目：{$new->project_name}】";
                break;
            case 'edit_admin':
                $type = LogMemberOp::TYPE_PROJECT_ADMIN;
                $oldMemberList = MemberService::getNormalListByIds(MemberProjectService::getAdminMemberIdsByProjectId($new->id));
                if (ArrayHelper::isArrEqual(array_column($oldMemberList, 'id'), array_column($model->getMemberList(), 'id')) === false) {
                    $oldMemberNames = implode(',', array_column($oldMemberList, 'realname'));
                    $memberNames = implode(',', array_column($model->getMemberList(), 'realname'));
                    $content = "编辑项目管理员;【项目：{$new->project_name}】由【管理员：{$oldMemberNames}】更改为【管理员：{$memberNames}】";
                }
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 部门操作日志
     * @param DepartmentForm $model
     * @param Department $new
     * @param Department|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createDepartmentLog(DepartmentForm $model, Department $new, ?Department $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($model->getScenario()) {
            case 'create':
            case 'edit':
                $pDepartmentName = $model->getParentDepartment()->department_name ?? '';
                $dataLevelStr = Department::$dataLevels[$new->data_level] ?? '';
                $selectRoleNames = implode(',', array_column(Role::findAll(['id' => $new->select_role_ids]), 'name'));
                $defaultRoleName = Role::findOne($new->default_role_id)->name ?? '';
                $leaderMemberNames = implode(',', array_column(Member::findAll(['id' => $new->leader_member_ids]), 'realname'));
                $optionStr = "【父部门：{$pDepartmentName} 部门：{$new->department_name} 数据权限：{$dataLevelStr} 角色：{$selectRoleNames} 默认角色：{$defaultRoleName} 部门负责人：{$leaderMemberNames} 排序：{$new->sort}】";
                if ($model->getScenario() == 'create') {
                    $type = LogMemberOp::TYPE_DEPARTMENT_CREATE;
                    $content = "新增部门;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_DEPARTMENT_UPDATE;
                    if (self::isEditModel($old, $new, ['department_name', 'data_level', 'default_role_id'])
                        || ArrayHelper::isArrEqual($new->select_role_ids, $old->select_role_ids) === false
                        || ArrayHelper::isArrEqual($new->leader_member_ids, $old->leader_member_ids) === false
                    ) {// 存在修改
                        $oldpDepartmentName = Department::findOne($old->pid)->department_name ?? '';
                        $oldDataLevelStr = Department::$dataLevels[$old->data_level] ?? '';
                        $oldSelectRoleNames = implode(',', array_column(Role::findAll(['id' => $old->select_role_ids]), 'name'));
                        $oldDefaultRoleName = Role::findOne($old->default_role_id)->name ?? '';
                        $oldLeaderMemberNames = implode(',', array_column(Member::findAll(['id' => $old->leader_member_ids]), 'realname'));
                        $content = "编辑部门;由【父部门：{$oldpDepartmentName} 部门：{$old->department_name} 数据权限：{$oldDataLevelStr} 角色：{$oldSelectRoleNames} 默认角色：{$oldDefaultRoleName} 部门负责人：{$oldLeaderMemberNames} 排序：{$old->sort}】更改为{$optionStr}";
                    }
                }
                break;
            case 'move':
                $type = LogMemberOp::TYPE_DEPARTMENT_MOVE;
                if (self::isEditModel($old, $new, ['pid', 'sort', 'default_role_id'])
                    || ArrayHelper::isArrEqual($new->select_role_ids, $old->select_role_ids) === false
                ) {
                    $pDepartmentName = $model->getParentDepartment()->department_name ?? '';
                    $oldpDepartmentName = Department::findOne($old->pid)->department_name ?? '';
                    $oldSelectRoleNames = implode(',', array_column(Role::findAll(['id' => $old->select_role_ids]), 'name'));
                    $oldDefaultRoleName = Role::findOne($old->default_role_id)->name ?? '';
                    $selectRoleNames = implode(',', array_column(Role::findAll(['id' => $new->select_role_ids]), 'name'));
                    $defaultRoleName = Role::findOne($new->default_role_id)->name ?? '';
                    $content = "转移部门;由【父部门：{$oldpDepartmentName} 部门：{$old->department_name} 角色：{$oldSelectRoleNames} 默认角色：{$oldDefaultRoleName} 排序：{$old->sort}】更改为【父部门：{$pDepartmentName} 部门：{$new->department_name} 角色：{$selectRoleNames} 默认角色：{$defaultRoleName} 排序：{$new->sort}】";
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_DEPARTMENT_DELETE;
                $content = "删除部门;【部门：{$new->department_name}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 岗位操作日志
     * @param JobForm $model
     * @param Job $new
     * @param Job|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createJobLog(JobForm $model, Job $new, ?Job $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($model->getScenario()) {
            case 'create':
            case 'edit':
                $statusStr = Job::$statuss[$new->status] ?? '';
                $optionStr = "【岗位：{$new->job_name} 排序：{$new->sort} 状态：{$statusStr}】";
                if ($model->getScenario() == 'create') {
                    $type = LogMemberOp::TYPE_JOB_CREATE;
                    $content = "新增岗位;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_JOB_UPDATE;
                    if (self::isEditModel($old, $new, ['job_name', 'sort', 'status'])) {// 存在修改
                        $oldStatusStr = Job::$statuss[$old->status] ?? '';
                        $content = "编辑岗位;由【岗位：{$old->job_name} 排序：{$old->sort} 状态：{$oldStatusStr}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_JOB_DELETE;
                $content = "删除岗位;【岗位：{$new->job_name}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 人员操作日志
     * @param MemberForm $model
     * @param Member $new
     * @param Member|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createMemberLog(MemberForm $model, Member $new, ?Member $old = null, ?Attr $newAttr = null, ?Attr $oldAttr = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($model->getScenario()) {
            case 'create':
            case 'edit':
                $statusStr = Member::$statuss[$new->status] ?? '';
                $projectNameArr = array_column($model->getProjectList(), 'project_name');
                $projectNameStr = $projectNameArr ? implode(',', $projectNameArr) : '';
                $departmentNameArr = array_column($model->getDepartmentList(), 'department_name');
                $departmentNameStr = $departmentNameArr ? implode(',', $departmentNameArr) : '';
                $roles = $model->getRoleList();
                $roleNameArr = array_column($roles, 'name');
                $roleNameStr = $roleNameArr ? implode(',', $roleNameArr) : '';
                $jobName = $model->getJob()->job_name ?? '';
                $optionStr = "【用户名：{$new->username} 手机号：{$new->mobile} 项目：{$projectNameStr} 部门：{$departmentNameStr} 角色：{$roleNameStr} 岗位：{$jobName} 状态：{$statusStr}】";
                if ($model->getScenario() == 'create') {
                    $type = LogMemberOp::TYPE_MEMBER_CREATE;
                    $content = "新增人员;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_MEMBER_UPDATE;
                    if (self::isEditModel($old, $new, ['username', 'mobile', 'status'])
                        || ArrayHelper::isArrEqual($newAttr->project_id, $oldAttr->project_id) === false
                        || ArrayHelper::isArrEqual($newAttr->department_id, $oldAttr->department_id) === false
                        || ArrayHelper::isArrEqual(array_column($roles, 'id'), $model->getOldRoleIds()) === false
                        || $newAttr->job_id != $oldAttr->job_id
                    ) {// 存在修改
                        $oldStatusStr = Job::$statuss[$old->status] ?? '';
                        $oldProjectNameArr = array_column(ProjectService::getNormalCacheListByIds($oldAttr->project_id), 'project_name');
                        $oldProjectNameStr = $oldProjectNameArr ? implode(',', $oldProjectNameArr) : '';
                        $oldDepartmentNameArr = array_column(DepartmentService::getNormalCacheListByIds($oldAttr->department_id), 'department_name');
                        $oldDepartmentNameStr = $oldDepartmentNameArr ? implode(',', $oldDepartmentNameArr) : '';
                        $oldRoles = RoleService::getNormalCacheListByIds($model->getOldRoleIds());
                        $oldRoleNameArr = array_column($oldRoles, 'name');
                        $oldRoleNameStr = $oldRoleNameArr ? implode(',', $oldRoleNameArr) : '';
                        $oldJobName = $oldAttr->job_id ? (RedisHelper::getJob($oldAttr->job_id)->job_name ?? '') : '';
                        $content = "编辑人员;由【用户名：{$old->username} 手机号：{$old->mobile} 项目：{$oldProjectNameStr} 部门：{$oldDepartmentNameStr} 角色：{$oldRoleNameStr} 岗位：{$oldJobName} 状态：{$oldStatusStr}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_MEMBER_DELETE;
                $content = "删除人员;【用户名：{$new->username}】";
                break;
            case 'update_password':
                $type = LogMemberOp::TYPE_MEMBER_UPDATE_PASSWORD;
                $content = "主动修改密码;【用户名：{$new->username}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 动作操作日志
     * @param ActionForm $model
     * @param Action $new
     * @param Action|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createActionLog(ActionForm $model, Action $new, ?Action $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($model->getScenario()) {
            case 'create':
            case 'edit':
                $statusStr = Action::$statuss[$new->status] ?? '';
                $menuName = $model->getMenu()->name ?? '';
                $optionStr = "【动作：{$new->name} 菜单：{$menuName} 路由：{$new->flag} 排序：{$new->sort} 状态：{$statusStr}】";
                if ($model->getScenario() == 'create') {
                    $type = LogMemberOp::TYPE_ACTION_CREATE;
                    $content = "新增动作;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_ACTION_UPDATE;
                    if (self::isEditModel($old, $new, ['menu_id', 'name', 'flag', 'sort', 'status'])) {// 存在修改
                        $oldStatusStr = Action::$statuss[$old->status] ?? '';
                        $oldMenuName = RedisHelper::getMenu($old->menu_id)->name ?? '';
                        $content = "编辑动作;由【动作：{$old->name} 菜单：{$oldMenuName} 路由：{$old->flag} 排序：{$old->sort} 状态：{$oldStatusStr}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_ACTION_DELETE;
                $content = "删除动作;【动作：{$new->name}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 菜单操作日志
     * @param MenuForm $model
     * @param Menu $new
     * @param Menu|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createMenuLog(MenuForm $model, Menu $new, ?Menu $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($model->getScenario()) {
            case 'create':
            case 'edit':
                $statusStr = Menu::$statuss[$new->status] ?? '';
                $pMenuName = $model->getParentMenu()->name ?? '';
                $optionStr = "【菜单：{$new->name} 父菜单：{$pMenuName} 菜单url：{$new->url} 菜单icon：{$new->class_style} 排序：{$new->sort} 状态：{$statusStr}】";
                if ($model->getScenario() == 'create') {
                    $type = LogMemberOp::TYPE_MENU_CREATE;
                    $content = "新增菜单;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_MENU_UPDATE;
                    if (self::isEditModel($old, $new, ['name', 'parent_menu_id', 'url', 'class_style', 'sort', 'status'])) {// 存在修改
                        $oldStatusStr = Menu::$statuss[$old->status] ?? '';
                        $oldPMenuName = RedisHelper::getMenu($old->parent_menu_id)->name ?? '';
                        $content = "编辑菜单;由【菜单：{$old->name} 父菜单：{$oldPMenuName} 菜单url：{$old->url} 菜单icon：{$old->class_style} 排序：{$old->sort} 状态：{$oldStatusStr}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_MENU_DELETE;
                $content = "删除菜单;【菜单：{$new->name}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 角色操作日志
     * @param RoleForm $model
     * @param Role $new
     * @param Role|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createRoleLog(RoleForm $model, Role $new, ?Role $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($model->getScenario()) {
            case 'create':
            case 'edit':
                $statusStr = Role::$statuss[$new->status] ?? '';
                $optionStr = "【角色：{$new->name} 描述：{$new->desc} 状态：{$statusStr}】";
                if ($model->getScenario() == 'create') {
                    $type = LogMemberOp::TYPE_ROLE_CREATE;
                    $content = "新增角色;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_ROLE_UPDATE;
                    if (self::isEditModel($old, $new, ['name', 'desc', 'status'])) {// 存在修改
                        $oldStatusStr = Role::$statuss[$old->status] ?? '';
                        $content = "编辑角色;由【角色：{$old->name} 描述：{$old->desc} 状态：{$oldStatusStr}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_ROLE_DELETE;
                $content = "删除角色;【角色：{$new->name}】";
                break;
            case 'edit_permission':
                $type = LogMemberOp::TYPE_ROLE_ALLOT;
                if ($model->getDeleteActionIds() || $model->getAddActionIds() || $model->getDeleteMenuIds() || $model->getAddMenuIds()) {
                    $delActions = ActionService::getNormalCacheListByIds($model->getDeleteActionIds());
                    $addActions = ActionService::getNormalCacheListByIds($model->getAddActionIds());
                    $delMenus = MenuService::getNormalCacheListByIds($model->getDeleteActionIds());
                    $addMenus = MenuService::getNormalCacheListByIds($model->getAddActionIds());

                    $delActionStr = $delActions ? implode(',', array_column($delActions, 'name')) : '';
                    $delMenuStr = $delMenus ? implode(',', array_column($delMenus, 'name')) : '';
                    $addActionStr = $addActions ? implode(',', array_column($addActions, 'name')) : '';
                    $addMenuStr = $addMenus ? implode(',', array_column($addMenus, 'name')) : '';
                    $content = "分配角色权限;【角色：{$new->name} 新增菜单：{$addMenuStr} 新增动作：{$addActionStr} 删除菜单：{$delMenuStr} 删除动作：{$delActionStr}】";
                }
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 工具操作日志
     * @param ToolForm $model
     * @param Tool $new
     * @param Tool|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createToolLog(ToolForm $model, Tool $new, ?Tool $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($model->getScenario()) {
            case 'create':
            case 'edit':
                $tagNameArr = array_column($model->getTagList(), 'name');
                $tagNameStr = implode(',', $tagNameArr);
                $categoryNameArr = array_column($model->getCategoryList(), 'name');
                $categoryNameStr = implode(',', $categoryNameArr);
                $ageName = $model->getAgeCategory()->name ?? '';
                $optionStr = "【工具名：{$new->name} 项目：{$model->getProject()->project_name} 简介：{$new->desc} 标签：{$tagNameStr} 年龄：{$ageName} 分类：{$categoryNameStr}】";
                if ($model->getScenario() == 'create') {
                    $type = LogMemberOp::TYPE_TOOL_CREATE;
                    $content = "新增工具;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_TOOL_UPDATE;
                    if (self::isEditModel($old, $new, ['project_id', 'name', 'desc', 'age_id'])
                        || $model->getAddTagIds()
                        || $model->getDelTagIds()
                        || $model->getAddCategoryIds()
                        || $model->getDelCategoryIds()
                    ) {// 存在修改
                        $oldPorjectName = RedisHelper::getProject($old->project_id)->project_name ?? '';
                        $oldTagNameArr = array_column($model->getOldTagList(), 'name');
                        $oldTagNameStr = implode(',', $oldTagNameArr);
                        $oldAgeName = RedisHelper::getToolCategory($old->age_id)->name ?? '';
                        $oldCategoryNameArr = array_column($model->getOldCategoryList(), 'name');
                        $oldCategoryNameStr = implode(',', $oldCategoryNameArr);
                        $content = "编辑工具;由【工具名：{$old->name} 项目：{$oldPorjectName} 简介：{$old->desc} 标签：{$oldTagNameStr} 年龄：{$oldAgeName} 分类：{$oldCategoryNameStr}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_TOOL_DELETE;
                $content = "删除工具;【工具名：{$new->name}】";
                break;
            case 'download':
                $type = LogMemberOp::TYPE_TOOL_DOWNLOAD;
                $msg = $model->download_type == 'attachment' ? '附件' : '文件';
                $file = $model->download_type == 'attachment' ? $model->file : implode(',', array_column($model->getDownloadToolFileList(), 'file'));
                $content = "下载{$msg};【工具名：{$new->name} 文件：{$file}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 工具水印操作日志
     * @param ToolWaterForm $model
     * @param ToolWater $new
     * @param ToolWater|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createToolWaterLog(ToolWaterForm $model, ToolWater $new, ?ToolWater $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($model->getScenario()) {
            case 'create':
            case 'edit':
                $typeStr = ToolWater::$types[$new->type];
                $optionStr = "【项目：{$model->getProject()->project_name} 类型：{$typeStr} 内容：{$new->text} 图片：{$new->file} 透明度：{$new->alpha}】";
                if ($model->getScenario() == 'create') {
                    $type = LogMemberOp::TYPE_TOOL_WATER_CREATE;
                    $content = "新增水印;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_TOOL_WATER_UPDATE;
                    if (self::isEditModel($old, $new, ['project_id', 'type', 'alpha'])
                        || ($new->type == $old->type && ($new->text != $old->text || $new->file != $old->file))
                    ) {// 存在修改
                        $oldTypeStr = ToolWater::$types[$new->type];
                        $oldProjectName = RedisHelper::getProject($old->project_id)->project_name ?? '';
                        $content = "编辑水印;由【项目：{$oldProjectName} 类型：{$oldTypeStr} 内容：{$old->text} 图片：{$old->file} 透明度：{$old->alpha}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_TOOL_WATER_DELETE;
                $projectName = RedisHelper::getProject($new->project_id)->project_name ?? '';
                $content = "删除水印;【项目：{$projectName}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 内容操作日志
     * @param ContentForm $model
     * @param Content $new
     * @param Content|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createContentLog(ContentForm $model, Content $new, ?Content $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($model->getScenario()) {
            case 'create':
            case 'edit':
                $ctype = Content::$types[$new->type];
                $keywordStr = $new->keyword ? implode(',', $new->keyword) : '';
                $isStreamStr = Content::$isStreams[$new->is_stream];
                $publishStatusStr = Content::$publishStatuss[$new->publish_status];
                $tagNameArr = array_column($model->getTagList(), 'name');
                $tagNameStr = implode(',', $tagNameArr);
                $doctorName = $model->getDoctor()->name ?? '';
                $optionStr = "【项目：{$model->getProject()->project_name} 类型：{$ctype} 标题：{$new->title} 内容：{$new->content} 医生：{$doctorName} 分类：{$model->getCommonStatus()->name} 关键词：{$keywordStr} 标签：{$tagNameStr} 投流：{$isStreamStr} 状态：{$publishStatusStr}】";
                if ($model->getScenario() == 'create') {
                    $type = LogMemberOp::TYPE_CONTENT_CREATE;
                    $content = "新增内容;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_CONTENT_UPDATE;
                    if (self::isEditModel($old, $new, ['project_id', 'type', 'title', 'content', 'doctor_id', 'c_id', 'is_stream', 'publish_status'])
                        || ArrayHelper::isArrEqual($new->keyword, $old->keyword) === false
                        || $model->getAddTagIds()
                        || $model->getDelTagIds()
                    ) {// 存在修改
                        $oldProject = RedisHelper::getProject($old->project_id);
                        $oldCtype = Content::$types[$old->type];
                        $oldDoctorName = Doctors::findOne($old->doctor_id)->name ?? '';
                        $oldCommonStatus = RedisHelper::getCommonStatus($old->c_id);
                        $oldKeywordStr = $old->keyword ? implode(',', $old->keyword) : '';
                        $oldIsStreamStr = Content::$isStreams[$old->is_stream];
                        $oldPublishStatusStr = Content::$publishStatuss[$old->publish_status];
                        $oldTagNameArr = array_column($model->getOldTagList(), 'name');
                        $oldTagNameStr = implode(',', $oldTagNameArr);
                        $content = "编辑内容;由【项目：{$oldProject->project_name} 类型：{$oldCtype} 标题：{$old->title} 内容：{$old->content} 医生：{$oldDoctorName} 分类：{$oldCommonStatus->name} 关键词：{$oldKeywordStr} 标签：{$oldTagNameStr} 投流：{$oldIsStreamStr} 状态：{$oldPublishStatusStr}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_CONTENT_DELETE;
                $content = "删除内容;【标题：{$new->title}】";
                break;
            case 'download':
                if ($model->is_zip) {
                    $type = LogMemberOp::TYPE_CONTENT_DOWNLOAD_ZIP;
                    $content = "下载ZIP;【标题：{$new->title}】";
                } else {
                    $type = LogMemberOp::TYPE_CONTENT_DOWNLOAD_VIDEO;
                    $content = "下载视频;【标题：{$new->title}】";
                }
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 发布内容操作日志
     * @param ContentPublishRecordForm $model
     * @param ContentPublishRecord $new
     * @param ContentPublishRecord|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createContentPublishRecordLog(ContentPublishRecordForm $model, ContentPublishRecord $new, ?ContentPublishRecord $old = null, ?Accounts $account = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($model->getScenario()) {
            case 'create':
                $type = LogMemberOp::TYPE_CONTENT_PUBLISH;
                $content = "发布内容;【医生：{$model->getDoctor()->name} 账号：{$account->nickname} 发布时间：{$model->publish_time}】";
                break;
            case 'edit_data':
                $type = LogMemberOp::TYPE_CONTENT_PUBLISH_UPDATE_DATA;
                if (self::isEditModel($old, $new, ['read_num', 'like_num', 'collect_num', 'comment_num'])) {// 存在修改
                    $newReadNum = CommonHelper::getNumFormat($new->read_num);
                    $newLikeNum = CommonHelper::getNumFormat($new->like_num);
                    $newCollectNum = CommonHelper::getNumFormat($new->collect_num);
                    $newCommentNum = CommonHelper::getNumFormat($new->comment_num);
                    $oldReadNum = CommonHelper::getNumFormat($old->read_num);
                    $oldLikeNum = CommonHelper::getNumFormat($old->like_num);
                    $oldCollectNum = CommonHelper::getNumFormat($old->collect_num);
                    $oldCommentNum = CommonHelper::getNumFormat($old->comment_num);
                    $optionStr = "【浏览量：{$newReadNum} 点赞：{$newLikeNum} 收藏：{$newCollectNum} 评论：{$newCommentNum}】";
                    $content = "更新发布内容数据;由【浏览量：{$oldReadNum} 点赞：{$oldLikeNum} 收藏：{$oldCollectNum} 评论：{$oldCommentNum}】更改为{$optionStr}";
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_CONTENT_PUBLISH_DELETE;
                $content = "删除内容数据;【内容ID：{$new->content_id}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 选题操作日志
     * @param TargetContentForm $model
     * @param TargetContent $new
     * @param TargetContent|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createTargetContentLog(TargetContentForm $model, TargetContent $new, ?TargetContent $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($model->getScenario()) {
            case 'create':
            case 'edit':
                $tTYpe = $model->getTCommonStatus()->name ?? '';
                $cTYpe = $model->getCCommonStatus()->name ?? '';
                $newLikeNum = CommonHelper::getNumFormat($new->like_num);
                $newCommentNum = CommonHelper::getNumFormat($new->comment_num);
                $newCollectNum = CommonHelper::getNumFormat($new->collect_num);
                $newForwardNum = CommonHelper::getNumFormat($new->forward_num);
                $optionStr = "【标题：{$new->title} 网址：{$new->url} 账号：{$new->account_name} 账号类型：{$tTYpe} 平台：{$new->platform_name} 点赞：{$newLikeNum} 评论：{$newCommentNum} 收藏：{$newCollectNum} 转发：{$newForwardNum} 分类：{$cTYpe} 话题：{$new->topic}】";
                if ($model->getScenario() == 'create') {
                    $type = LogMemberOp::TYPE_TARGET_CONTENT_CREATE;
                    $content = "新增选题;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_TARGET_CONTENT_UPDATE;
                    if (self::isEditModel($old, $new, ['title', 'url', 'account_name', 't_id', 'platform_name', 'like_num', 'comment_num', 'collect_num', 'forward_num', 'c_id', 'topic'])) {// 存在修改
                        $oldTTYpe = RedisHelper::getCommonStatus($old->t_id ?? 0)->name ?? '';
                        $oldCTYpe = RedisHelper::getCommonStatus($old->c_id ?? 0)->name ?? '';
                        $oldLikeNum = CommonHelper::getNumFormat($old->like_num);
                        $oldCommentNum = CommonHelper::getNumFormat($old->comment_num);
                        $oldCollectNum = CommonHelper::getNumFormat($old->collect_num);
                        $oldForwardNum = CommonHelper::getNumFormat($old->forward_num);
                        $content = "编辑选题;由【标题：{$old->title} 网址：{$old->url} 账号：{$old->account_name} 账号类型：{$oldTTYpe} 平台：{$old->platform_name} 点赞：{$oldLikeNum} 评论：{$oldCommentNum} 收藏：{$oldCollectNum} 转发：{$oldForwardNum} 分类：{$oldCTYpe} 话题：{$old->topic}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_TARGET_CONTENT_DELETE;
                $content = "删除选题;【标题：{$new->title}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 选题导入操作日志
     * @param array $data
     */
    public static function createTargetContentUploadLog(array $data): ?LogMemberOp
    {
        $type = LogMemberOp::TYPE_TARGET_CONTENT_UPLOAD;
        $cnt = count($data);
        $content = "导入选题;【导入总数：{$cnt}】";
        return self::create(0, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 手机卡操作日志
     * @param string $scenario
     * @param Scard $new
     * @param Scard|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createScardLog(string $scenario, ?Scard $new = null, ?Scard $old = null, ?ScardRecharge $newRecharge = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($scenario) {
            case 'create':
            case 'edit':
                $newProject = RedisHelper::getProject($new->project_id ?? 0)->project_name ?? '';
                $newSimTelecom = $new->sim_telecom ? Scard::$simTelecoms[$new->sim_telecom] : '';
                $newStatusName = Scard::$statuss[$new->status];
                $optionStr = "【手机卡号：{$new->sim_card} 所属项目：{$newProject} 运营商：{$newSimTelecom} 认证人：{$new->authenticator_user} 备注：{$new->remark} 状态：{$newStatusName}】";
                if ($scenario == 'create') {
                    $type = LogMemberOp::TYPE_SCARD_CREATE;
                    $content = "新增手机卡;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_SCARD_UPDATE;
                    $oldProject = RedisHelper::getProject($old->project_id ?? 0)->project_name ?? '';
                    $oldSimTelecom = $old->sim_telecom ? Scard::$simTelecoms[$old->sim_telecom] : '';
                    $oldStatusName = Scard::$statuss[$old->status];
                    if (self::isEditModel($old, $new, ['sim_card', 'project_id', 'sim_telecom', 'authenticator_user', 'remark', 'status'])) {// 存在修改
                        $content = "编辑手机卡;由【手机卡号：{$old->sim_card} 所属项目：{$oldProject} 运营商：{$oldSimTelecom} 认证人：{$old->authenticator_user} 备注：{$old->remark} 状态：{$oldStatusName}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_SCARD_DELETE;
                $content = "删除手机卡;【手机卡：{$new->sim_card}】";
                break;
            case 'recharge':
                $type = LogMemberOp::TYPE_SCARD_RECHARGE;
                $newRechargeDate = date('Y-m-d', $newRecharge->recharge_time);
                $content = "手机卡充值;【套餐：{$newRecharge->remark} 日期：{$newRechargeDate} 金额：{$newRecharge->recharge_money}】";
                break;
            case 'delete_recharge':
                $type = LogMemberOp::TYPE_SCARD_DELETE_RECHARGE;
                $newRechargeDate = date('Y-m-d', $newRecharge->recharge_time);
                $content = "删除充值;【账号：{$new->sim_card} 金额：{$newRecharge->recharge_money} 日期：{$newRechargeDate}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        if ($scenario == 'recharge') {
            $newId = $newRecharge->scard_id;
        } else {
            $newId = $new->id;
        }
        return self::create($newId, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 设备操作日志
     * @param string $scenario
     * @param Devices $new
     * @param Devices|null $old
     * @param array|null $newMobileArr
     * @param array|null $oldMobileArr
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createDeviceLog(string $scenario, Devices $new, ?Devices $old = null, array $newMobileList = null, array $oldMobileList = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($scenario) {
            case 'create':
            case 'edit':
                $newMobileArr = [];
                if ($newMobileList) {
                    foreach ($newMobileList as $ma) {
                        $newMobileArr[] = ScardService::getData($ma)->sim_card;
                    }
                }
                $newMobile = implode(',', $newMobileArr);
                $newProject = RedisHelper::getProject($new->project_id ?? 0)->project_name ?? '';
                $newStatusName = Devices::$statuss[$new->status];
                $optionStr = "【手机型号：{$new->phone_model} 设备名称：{$new->name} 手机号：{$newMobile} 所属项目：{$newProject} 备注：{$new->remark} 状态：{$newStatusName}】";
                if ($scenario == 'create') {
                    $type = LogMemberOp::TYPE_DEVICE_CREATE;
                    $content = "新增设备;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_DEVICE_UPDATE;
                    $oldMobileArr = [];
                    if ($oldMobileList) {
                        foreach ($oldMobileList as $ma) {
                            $oldMobileArr[] = ScardService::getData($ma)->sim_card;
                        }
                    }
                    $oldMobile = implode(',', $oldMobileArr);
                    $oldProject = RedisHelper::getProject($old->project_id ?? 0)->project_name ?? '';
                    $oldStatusName = Devices::$statuss[$old->status];
                    if (
                        (self::isEditModel($old, $new, ['phone_model', 'name', 'project_id', 'use_mid', 'remark', 'status'])) ||
                        (ArrayHelper::isArrEqual($newMobileArr, $oldMobileArr) === false)
                    ) {// 存在修改
                        $content = "编辑设备;由【手机型号：{$old->phone_model} 设备名称：{$old->name} 手机号：{$oldMobile} 所属项目：{$oldProject} 备注：{$old->remark} 状态：{$oldStatusName}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_DEVICE_DELETE;
                $content = "删除设备;【设备名：{$new->name}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 账号操作日志
     * @param string $scenario
     * @param Accounts $new
     * @param Accounts|null $old
     * @param AccountsFans|null $newFans
     * @param AccountsConsume|null $newConsume
     * @param AccountsConsume|null $oldConsume
     * @param AccountsConsumeData|null $newConsumeData
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createAccountLog(string $scenario, Accounts $new, ?Accounts $old = null, ?AccountsFans $newFans = null, ?AccountsConsume $newConsume = null, ?AccountsConsume $oldConsume = null, ?AccountsConsumeData $newConsumeData = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($scenario) {
            case 'create':
            case 'edit':
                $newProject = RedisHelper::getProject($new->project_id ?? 0)->project_name ?? '';
                $newPlatform = RedisHelper::getPlatform($new->platform_id ?? 0)->name ?? '';
                $newType = RedisHelper::getCommonStatus($new->t_id ?? 0)->name ?? '';
                $newDevices = DevicesService::getData(intval($new->login_device_id ?? 0))->name ?? '';
                $newDoctors = DoctorsService::getData(intval($new->doctor_id ?? 0))->name ?? '';
                $newIsSteam = Accounts::$streams[$new->is_stream];
                $newStatus = Accounts::$statuss[$new->status];
                $useMname = RedisHelper::getMember($new->use_mid ?? 0)->realname ?? '';
                $optionStr = "【项目：{$newProject} 平台：{$newPlatform} 账号ID：{$new->account_name} 昵称：{$new->nickname} 类型：{$newType} 登录设备：{$newDevices} 认证医生：{$newDoctors} 投流：{$newIsSteam} 登录手机号：{$new->login_account} 登录密码：{$new->login_passwd} 运营人：{$useMname} 注册日期：{$new->accounts_reg_date} 状态：{$newStatus}】";
                if ($scenario == 'create') {
                    $type = LogMemberOp::TYPE_ACCOUNT_CREATE;
                    $content = "新增账号;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_ACCOUNT_UPDATE;
                    $oldProject = RedisHelper::getProject($old->project_id ?? 0)->project_name ?? '';
                    $oldPlatform = RedisHelper::getPlatform($old->platform_id ?? 0)->name ?? '';
                    $oldType = RedisHelper::getCommonStatus($old->t_id ?? 0)->name ?? '';
                    $oldDevices = DevicesService::getData(intval($old->login_device_id ?? 0))->name ?? '';
                    $oldDoctors = DoctorsService::getData(intval($old->doctor_id ?? 0))->name ?? '';
                    $oldIsSteam = Accounts::$streams[$old->is_stream];
                    $oldStatusName = Accounts::$statuss[$old->status];
                    $oldUseMname = RedisHelper::getMember($old->use_mid ?? 0)->realname ?? '';
                    if (self::isEditModel($old, $new, ['project_id', 'platform_id', 'nickname', 't_id', 'login_device_id', 'doctor_id', 'is_stream', 'login_account', 'login_passwd', 'use_mid', 'accounts_reg_date', 'status'])) {// 存在修改
                        $content = "编辑账号;由【项目：{$oldProject} 平台：{$oldPlatform}  账号ID：{$old->account_name} 昵称：{$old->nickname} 类型：{$oldType} 登录设备：{$oldDevices} 认证医生：{$oldDoctors} 投流：{$oldIsSteam} 登录手机号：{$new->login_account} 登录密码：{$new->login_passwd} 运营人：{$oldUseMname} 注册日期：{$new->accounts_reg_date} 状态：{$oldStatusName}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_ACCOUNT_DELETE;
                $content = "删除账号;【账号名：{$new->nickname}】";
                break;
            case 'update_fans':
                $type = LogMemberOp::TYPE_ACCOUNT_UPDATE_FANS;
                $content = "更新账号粉丝;【账号：{$new->nickname} 粉丝数：{$newFans->fans_num}】";
                break;
            case 'update_principal':
                $type = LogMemberOp::TYPE_ACCOUNT_UPDATE_PRINCIPAL;
                $newUseMname = RedisHelper::getMember($new->use_mid ?? 0)->realname ?? '';
                $content = "批量设置运营;【账号：{$new->nickname} 设置运营人为：{$newUseMname} 】";
                break;
            case 'create_consume':
                $type = LogMemberOp::TYPE_ACCOUNT_CREATE_CONSUME;
                $dateTime = date('Y-m-d', $newConsume->date_time);
                $newTypeName = AccountsConsume::$types[$newConsume->type];
                $content = "消费;【账号：{$new->nickname} 金额：{$newConsume->money} 日期：{$dateTime} 投放地区：{$newConsume->city_name} 时长：{$newConsume->hours} 投放类型：{$newTypeName} 投放时间：{$newConsume->hour_minute}】";
                break;
            case 'update_consume':
                $type = LogMemberOp::TYPE_ACCOUNT_UPDATE_CONSUME;
                $oldTypeName = AccountsConsume::$types[$oldConsume->type];
                $oldDate = date('Y-m-d', $oldConsume->date_time);
                $newTypeName = AccountsConsume::$types[$newConsume->type];
                $newDate = date('Y-m-d', $newConsume->date_time);
                if (self::isEditModel($oldConsume, $newConsume, ['money', 'date_time', 'city_id', 'hours', 'type', 'hour_minute'])) {// 存在修改
                    $content = "编辑账号;由【账号：{$new->nickname} 金额：{$oldConsume->money} 日期：{$oldDate} 投放地区：{$oldConsume->city_name} 时长：{$oldConsume->hours} 投放类型：{$oldTypeName} 投放时间：{$oldConsume->hour_minute}】更改为【账号：{$new->nickname} 金额：{$newConsume->money} 日期：{$newDate} 投放地区：{$newConsume->city_name} 时长：{$newConsume->hours} 投放类型：{$newTypeName} 投放时间：{$newConsume->hour_minute}】";
                }
                break;
            case 'update_consume_data':
                $type = LogMemberOp::TYPE_ACCOUNT_UPDATE_CONSUME_DATA;
                $content = "消费后的数据;【有效私信：{$newConsumeData->private_sms_num} 有效线索：{$newConsumeData->effective_clue_num} 留电数：{$newConsumeData->give_mobile_num} 推荐：{$newConsumeData->recommend_num}】";
                break;
            case 'delete_consume':
                $type = LogMemberOp::TYPE_ACCOUNT_DELETE_CONSUME;
                $dateTime = date('Y-m-d', $newConsume->date_time);
                $privateSmsNum = $newConsumeData ? $newConsumeData->private_sms_num : '';
                $effectiveClueNum = $newConsumeData ? $newConsumeData->effective_clue_num : '';
                $giveMobileNum = $newConsumeData ? $newConsumeData->give_mobile_num : '';
                $content = "删除消费;【账号：{$new->nickname} 金额：{$newConsume->money} 日期：{$dateTime}】【有效私信：{$privateSmsNum} 有效线索：{$effectiveClueNum} 留电数：{$giveMobileNum}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 平台操作日志
     * @param string $scenario
     * @param Platform $new
     * @param Platform|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createPlatformLog(string $scenario, Platform $new, ?Platform $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($scenario) {
            case 'create':
            case 'edit':
                $typeArr = is_array($new->platform_content_type) ? $new->platform_content_type : explode(',', CommonHelper::strFormat($new->platform_content_type));
                $newTypeArr = [];
                foreach ($typeArr as $ta) {
                    $newTypeArr[] = Platform::$types[$ta];
                }
                $newType = implode(',', $newTypeArr);
                $newStatus = Platform::$statuss[$new->status];
                $optionStr = "【名称：{$new->name} 内容类型：{$newType} 认证规则：{$new->authenticator_rules} 状态：{$newStatus}】";
                if ($scenario == 'create') {
                    $type = LogMemberOp::TYPE_PLATFORM_CREATE;
                    $content = "新增平台;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_PLATFORM_UPDATE;
                    $typeArr = is_array($old->platform_content_type) ? $old->platform_content_type : explode(',', CommonHelper::strFormat($old->platform_content_type));
                    $oldTypeArr = [];
                    foreach ($typeArr as $ta) {
                        $oldTypeArr[] = Platform::$types[$ta];
                    }
                    $oldType = implode(',', $oldTypeArr);
                    $oldStatus = Platform::$statuss[$old->status];
                    if (self::isEditModel($old, $new, ['name', 'platform_content_type', 'authenticator_rules', 'status'])) {// 存在修改
                        $content = "编辑平台;由【名称：{$old->name} 内容类型：{$oldType} 认证规则：{$old->authenticator_rules} 状态：{$oldStatus}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_PLATFORM_DELETE;
                $content = "删除平台;【平台名：{$new->name}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 医生操作日志
     * @param string $scenario
     * @param Doctors $new
     * @param Doctors|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createDoctorsLog(string $scenario, Doctors $new, ?Doctors $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($scenario) {
            case 'create':
            case 'edit':
                $newPopStars = $new->pop_stars ? Doctors::$popStars[$new->pop_stars] : '';
                $newType = Doctors::$types[$new->type];
                $newProject = RedisHelper::getProject($new->project_id ?? 0)->project_name ?? '';
                $newStatus = Doctors::$statuss[$new->status];
                $optionStr = "【医生：{$new->name} 推广星级：{$newPopStars} 医院：{$new->hospital_name} 医院类型：{$newType} 项目：{$newProject} 状态：{$newStatus}】";
                if ($scenario == 'create') {
                    $type = LogMemberOp::TYPE_DOCTOR_CREATE;
                    $content = "新增医生;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_DOCTOR_UPDATE;
                    $oldPopStars = $old->pop_stars ? Doctors::$popStars[$old->pop_stars] : '';
                    $oldType = Doctors::$types[$old->type];
                    $oldProject = RedisHelper::getProject($old->project_id ?? 0)->project_name ?? '';
                    $oldStatus = Doctors::$statuss[$old->status];
                    if (self::isEditModel($old, $new, ['name', 'pop_stars', 'hospital_name', 'type', 'project_id', 'status'])) {// 存在修改
                        $content = "编辑医生;由【医生：{$old->name} 推广星级：{$oldPopStars} 医院：{$old->hospital_name} 医院类型：{$oldType} 项目：{$oldProject} 状态：{$oldStatus}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_DOCTOR_DELETE;
                $content = "删除医生;【医生名：{$new->name}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 对标账号操作日志
     * @param string $scenario
     * @param TargetAccounts $new
     * @param TargetAccounts|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createTargetAccountsLog(string $scenario, TargetAccounts $new, ?TargetAccounts $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($scenario) {
            case 'create':
            case 'edit':
                $newAccount = AccountsService::getData($new->accounts_id ?? 0)->nickname ?? '';
                $newType = RedisHelper::getCommonStatus($new->t_id ?? 0)->name ?? '';
                $newPlatform = PlatformService::getData($new->platform_id ?? 0)->name ?? '';
                $newStatus = TargetAccounts::$statuss[$new->status];
                $optionStr = "【系统账号：{$newAccount} 对标账号：{$new->name} 账号类型：{$newType} 人设定位：{$new->position} 平台：{$newPlatform} 粉丝数：{$new->fans_num} 作品数：{$new->works_num} 状态：{$newStatus}】";
                if ($scenario == 'create') {
                    $type = LogMemberOp::TYPE_TARGET_ACCOUNT_CREATE;
                    $content = "新增对标账号;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_TARGET_ACCOUNT_UPDATE;
                    $oldAccount = AccountsService::getData($old->accounts_id ?? 0)->nickname ?? '';
                    $oldType = RedisHelper::getCommonStatus($old->t_id ?? 0)->name ?? '';
                    $oldPlatform = PlatformService::getData($old->platform_id ?? 0)->name ?? '';
                    $oldStatus = TargetAccounts::$statuss[$old->status];
                    if (self::isEditModel($old, $new, ['accounts_id', 'name', 't_id', 'position', 'platform_id', 'fans_num', 'works_num', 'status'])) {// 存在修改
                        $content = "编辑对标账号;由【系统账号：{$oldAccount} 对标账号：{$old->name} 账号类型：{$oldType} 人设定位：{$old->position} 平台：{$oldPlatform} 粉丝数：{$old->fans_num} 作品数：{$old->works_num} 状态：{$oldStatus}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_TARGET_ACCOUNT_DELETE;
                $content = "删除对标账号;【对标账号：{$new->name}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 业务数据操作日志
     * @param string $scenario
     * @param AccountsData $new
     * @param AccountsData|null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createAccountsDataLog(string $scenario, AccountsData $new, ?AccountsData $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($scenario) {
            case 'create':
            case 'edit':
                $newPlatform = PlatformService::getData($new->platform_id ?? 0)->name ?? '';
                $newAccount = AccountsService::getData($new->account_id ?? 0)->nickname ?? '';
                $optionStr = "【日期：{$new->date_time} 平台：{$newPlatform} 账号：{$newAccount} 私信：{$new->private_sms} 线索：{$new->clue} 有效线索：{$new->effective_clue} 到诊：{$new->arrive}】";
                if ($scenario == 'create') {
                    $type = LogMemberOp::TYPE_ACCOUNT_DATA_CREATE;
                    $content = "新增业务数据;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_ACCOUNT_DATA_UPDATE;
                    $oldPlatform = PlatformService::getData($old->platform_id ?? 0)->name ?? '';
                    $oldAccount = AccountsService::getData($old->account_id ?? 0)->nickname ?? '';
                    if (self::isEditModel($old, $new, ['date_time', 'platform_id', 'account_id', 'private_sms', 'clue', 'effective_clue', 'arrive'])) {// 存在修改
                        $content = "编辑业务数据;由【日期：{$old->date_time} 平台：{$oldPlatform} 账号：{$oldAccount} 私信：{$old->private_sms} 线索：{$old->clue} 有效线索：{$old->effective_clue} 到诊：{$old->arrive}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_ACCOUNT_DATA_DELETE;
                $newPlatform = PlatformService::getData($new->platform_id ?? 0)->name ?? '';
                $newAccount = AccountsService::getData($new->account_id ?? 0)->nickname ?? '';
                $content = "删除业务数据;【日期：{$new->date_time} 平台：{$newPlatform} 账号：{$newAccount}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 基础设置操作日志
     * @param string $scenario
     * @param CommonStatus $new
     * @param CommonStatus |null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createCommonStatusLog(string $scenario, CommonStatus $new, ?CommonStatus $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($scenario) {
            case 'create':
            case 'edit':
                $newStatus = CommonStatus::$statuss[$new->status];
                $optionStr = "【名称：{$new->name} 排序：{$new->sort} 状态：{$newStatus}】";
                if ($scenario == 'create') {
                    $type = LogMemberOp::TYPE_COMMONS_STATUS_CREATE;
                    $content = "新增基础设置;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_COMMONS_STATUS_UPDATE;
                    $oldStatus = CommonStatus::$statuss[$old->status];
                    if (self::isEditModel($old, $new, ['name', 'sort', 'status'])) {// 存在修改
                        $content = "编辑基础设置;由【名称：{$old->name} 排序：{$old->sort} 状态：{$oldStatus}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_COMMONS_STATUS_DELETE;
                $content = "删除基础设置;【名称:{$new->name}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 标签操作日志
     * @param string $scenario
     * @param Tags $new
     * @param Tags |null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createTagsLog(string $scenario, Tags $new, ?Tags $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($scenario) {
            case 'create':
            case 'edit':
                $newType = Tags::$types[$new->type];
                $optionStr = "【标签名称：{$new->name} 类型：{$newType}】";
                if ($scenario == 'create') {
                    $type = LogMemberOp::TYPE_TAGS_CREATE;
                    $content = "新增标签;{$optionStr}";
                } else {
                    $type = LogMemberOp::TYPE_TAGS_UPDATE;
                    $oldType = Tags::$types[$old->type];
                    if (self::isEditModel($old, $new, ['name', 'type'])) {// 存在修改
                        $content = "编辑标签;由【标签名称：{$old->name} 类型：{$oldType}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_TAGS_DELETE;
                $content = "删除标签;【标签名称：{$new->name}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }

    /**
     * 素材分类操作日志
     * @param string $scenario
     * @param ToolCategory $new
     * @param ToolCategory |null $old
     * @return LogMemberOp|null
     * @throws \Exception
     */
    public static function createToolCategoryLog(string $scenario, ToolCategory $new, ?ToolCategory $old = null): ?LogMemberOp
    {
        $content = '';
        $type = 0;
        switch ($scenario) {
            case 'create':
            case 'edit':
                $newStatus = ToolCategory::$statuss[$new->status];
                $optionStr = "【素材分类名称：{$new->name} 状态：{$newStatus} 父级ID：{$new->pid}】";
                if ($scenario == 'create') {
                    $type = LogMemberOp::TYPE_TOOL_CATEGORY_CREATE;
                    $content = "新增素材分类;{$optionStr}";
                } else {
                    $oldStatus = ToolCategory::$statuss[$old->status];
                    $type = LogMemberOp::TYPE_TOOL_CATEGORY_UPDATE;
                    if (self::isEditModel($old, $new, ['name', 'status', 'pid'])) {// 存在修改
                        $content = "编辑素材分类;由【素材分类名称：{$old->name} 状态：{$oldStatus} 父级ID：{$old->pid}】更改为{$optionStr}";
                    }
                }
                break;
            case 'delete':
                $type = LogMemberOp::TYPE_TOOL_CATEGORY_DELETE;
                $content = "删除素材分类;【素材分类名称：{$new->name}】";
                break;
            default:
                break;
        }
        if (empty($content) || empty($type)) {
            return null;
        }
        return self::create($new->id, $content, $type, Yii::$app->user->identity->member_id);
    }
}